package com.chushouya.controller.product.admin;

import com.chushouya.common.constant.ServiceType;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.general.framework.web.annotations.RepeatSubmit;
import com.chushouya.product.dto.admin.category.CategoryDetailDTO;
import com.chushouya.product.dto.admin.category.CategoryListDTO;
import com.chushouya.product.dto.admin.category.CategoryQuery;
import com.chushouya.product.service.admin.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = ServiceType.PLATFORM + "商品分类管理")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation("获取商品分类列表")
    @GetMapping("/list")
    public Response<List<CategoryListDTO>> list(CategoryQuery query) {
        return Response.success(categoryService.selectCategoryList(query));
    }

    @ApiOperation("根据categoryId获取详细信息")
    @GetMapping("/detail")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类目id", required = true),
    })
    public Response<CategoryDetailDTO> getDetail(Long categoryId) {
        return Response.success(categoryService.getCategoryDetail(categoryId));
    }

    @ApiOperation("新增商品分类")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @RepeatSubmit
    public Response<Long> add(@Validated @RequestBody CategoryDetailDTO category) {
        return Response.success(categoryService.addCategory(category));
    }

    @ApiOperation("修改商品分类")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<Long> edit(@Validated @RequestBody CategoryDetailDTO category) {
        return Response.success(categoryService.editCategory(category));
    }

    @ApiOperation("修改分类状态")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/editStatus")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类目id", required = true),
            @ApiImplicitParam(name = "status", value = "状态 0下架 1上架", required = true),
    })
    public Response<Long> editStatus(Long categoryId, Integer status) {
        return Response.success(categoryService.editCategoryStatus(categoryId, status));
    }

    @ApiOperation("删除商品分类")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId", value = "类目id", required = true)
    })
    public Response<Void> remove(Long categoryId) {
        categoryService.removeCategory(categoryId);
        return Response.success();
    }
}
